#!/bin/bash
set -e

echo ">>> Running build-java.sh for multi-JDK packaging"

VERSIONS=${JAVA_PACKAGE_VERSIONS:-"17 21"}
BUILDROOT=$1
NAME=$2
SRCDIR=$3

for version in $VERSIONS; do
echo ">>> Building for JDK $version"
export JAVA_HOME="/usr/lib/jvm/java-${version}-openjdk"

mkdir -p "build-jdk${version}"
$JAVA_HOME/bin/javac -d "build-jdk${version}" "$SRCDIR"/HelloWorld.java

install_path="${BUILDROOT}/usr/share/${NAME}-jdk${version}"
mkdir -p "$install_path"
cp "build-jdk${version}"/HelloWorld.class "$install_path/"

echo "/usr/share/${NAME}-jdk${version}/HelloWorld.class" > "$SRCDIR/files-jdk${version}.list"
done
